package com.fr.data.core.db.dialect;

import com.fr.base.StringUtils;
import com.fr.data.core.db.TableProcedure;
import com.fr.data.core.db.dml.Table;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AbstractDialect
  implements Dialect
{
  protected String left_quote = null;
  protected String right_quote = null;
  private static int fetchSize = -1;

  public int getFetchSize()
  {
    return fetchSize;
  }

  public String column2SQL(String paramString)
  {
    if (StringUtils.isBlank(paramString))
      return null;
    if ((StringUtils.isBlank(this.left_quote)) || (StringUtils.isBlank(this.right_quote)))
      return paramString;
    return this.left_quote + paramString.trim().toUpperCase() + this.right_quote;
  }

  public String columnType2SQL(int paramInt, String paramString)
  {
    return TypeUtils.getTypeName(paramInt);
  }

  public String table2SQL(Table paramTable)
  {
    if (paramTable == null)
      return "";
    if ((StringUtils.isBlank(this.left_quote)) || (StringUtils.isBlank(this.right_quote)))
      return paramTable.toString();
    StringBuffer localStringBuffer = new StringBuffer();
    String str = paramTable.getSchema();
    if (!(StringUtils.isBlank(str)))
      localStringBuffer.append(column2SQL(str)).append(".");
    localStringBuffer.append(column2SQL(paramTable.getName()));
    return localStringBuffer.toString();
  }

  public String[] getSchemas(Connection paramConnection)
    throws Exception
  {
    DatabaseMetaData localDatabaseMetaData = paramConnection.getMetaData();
    ArrayList localArrayList = new ArrayList();
    if (!(StringUtils.isBlank(localDatabaseMetaData.getSchemaTerm())))
    {
      ResultSet localResultSet = localDatabaseMetaData.getSchemas();
      while (localResultSet.next())
        localArrayList.add(localResultSet.getString(1).trim());
    }
    return ((String[])localArrayList.toArray(new String[localArrayList.size()]));
  }

  public List getTableProcedure(Connection paramConnection, String paramString, boolean paramBoolean)
    throws Exception
  {
    if (paramConnection == null)
      throw new Exception("Cannot connect to database!");
    ArrayList localArrayList = new ArrayList();
    String str = null;
    if (paramString != null)
      str = paramConnection.getCatalog();
    DatabaseMetaData localDatabaseMetaData = paramConnection.getMetaData();
    String[] arrayOfString = { "TABLE" };
    ResultSet localResultSet = null;
    try
    {
      localResultSet = localDatabaseMetaData.getTables(str, paramString, "%", arrayOfString);
    }
    catch (SQLException localSQLException)
    {
      localResultSet = localDatabaseMetaData.getTables(str, paramString, "%", arrayOfString);
    }
    while (localResultSet.next())
      localArrayList.add(new TableProcedure(localResultSet.getString(2), localResultSet.getString(3), localResultSet.getString(4)));
    localResultSet.close();
    return localArrayList;
  }

  public String createSequence(Connection paramConnection, String paramString1, String paramString2, String paramString3)
  {
    return paramString3;
  }

  public String getIdentitySelectString(String paramString1, String paramString2, int paramInt)
    throws Exception
  {
    return getIdentitySelectString();
  }

  protected String getIdentitySelectString()
    throws Exception
  {
    throw new Exception("Dialect does not support identity key generation");
  }

  public void setAutoCommit(Connection paramConnection, boolean paramBoolean)
    throws SQLException
  {
    paramConnection.setAutoCommit(paramBoolean);
  }

  public String getLimitString(String paramString, int paramInt1, int paramInt2)
  {
    throw new UnsupportedOperationException("paged queries not supported");
  }

  public boolean supportsLimit()
  {
    return false;
  }

  public boolean supportsLimitOffset()
  {
    return supportsLimit();
  }

  public String getCountSql(String paramString)
  {
    return "SELECT COUNT(*) AS rowcount FROM (" + paramString + ") t";
  }

  public boolean isYearData(Connection paramConnection, int paramInt, Table paramTable, String paramString)
  {
    return false;
  }

  public String quartzDelegateClass()
  {
    return "com.fr.third.org.quartz.impl.jdbcjobstore.StdJDBCDelegate";
  }
}